package conv

import (
	"bp/db/sqlc/ctype"
	"bp/internal/service/param"
	"bp/internal/util"
	messageGrpc "bp/rpc/gen/shared/message"
)

// ConvTimeRangeArrayEntityToPb []ctype.TimeRange => []*messageGrpc.TimeRange
func ConvTimeRangeArrayEntityToPb(params ctype.TimeRangeArray) (vals []*messageGrpc.TimeRange) {
	for _, p := range params {
		vals = append(vals, &messageGrpc.TimeRange{
			BeginAt: p.BeginAt,
			EndAt:   p.EndAt,
		})
	}
	return
}

// ConvTimeRangeArrayPbToEntity []*messageGrpc.TimeRange => []ctype.TimeRange
func ConvTimeRangeArrayPbToEntity(params []*messageGrpc.TimeRange) (vals ctype.TimeRangeArray) {
	for _, p := range params {
		vals = append(vals, ctype.TimeRange{
			BeginAt: p.BeginAt,
			EndAt:   p.EndAt,
		})
	}
	return
}

// ConvItemCategoryConfigurationPtrArrayEntityToPb []*param.ItemCategoryConfiguration => []*messageGrpc.ItemCategoryConfiguration
func ConvItemCategoryConfigurationPtrArrayEntityToPb(params param.ItemCategoryConfigurationPtrArray) (vals []*messageGrpc.ItemCategoryConfiguration) {
	for _, p := range params {
		vals = append(vals, &messageGrpc.ItemCategoryConfiguration{
			Id:   p.ID,
			Name: p.Name,
		})
	}
	return
}

// ConvItemCategoryConfigurationPtrArrayPbToEntity []*messageGrpc.ItemCategoryConfiguration => []*param.ItemCategoryConfiguration
func ConvItemCategoryConfigurationPtrArrayPbToEntity(params []*messageGrpc.ItemCategoryConfiguration) (vals param.ItemCategoryConfigurationPtrArray) {
	for _, p := range params {
		vals = append(vals, &param.ItemCategoryConfiguration{
			ID:   p.Id,
			Name: p.Name,
		})
	}
	return
}

// ConvItemConfigurationPtrArrayEntityToPb []*param.ItemConfiguration => []*messageGrpc.ItemConfiguration
func ConvItemConfigurationPtrArrayEntityToPb(params param.ItemConfigurationPtrArray) (vals []*messageGrpc.ItemConfiguration) {
	for _, p := range params {
		vals = append(vals, &messageGrpc.ItemConfiguration{
			Id:   p.ID,
			Name: p.Name,
		})
	}
	return
}

// ConvItemConfigurationPtrArrayPbToEntity []*messageGrpc.ItemConfiguration => []*param.ItemConfiguration
func ConvItemConfigurationPtrArrayPbToEntity(params []*messageGrpc.ItemConfiguration) (vals param.ItemConfigurationPtrArray) {
	for _, p := range params {
		vals = append(vals, &param.ItemConfiguration{
			ID:   p.Id,
			Name: p.Name,
		})
	}
	return
}

// ConvOffGradientRuleArrayEntityToPb []ctype.OffGradientRule => []*messageGrpc.OffGradientRule
func ConvOffGradientRuleArrayEntityToPb(params ctype.OffGradientRuleArray) (vals []*messageGrpc.OffGradientRule) {
	for _, p := range params {
		vals = append(vals, &messageGrpc.OffGradientRule{
			Price:      p.Price,
			Quota:      p.Quota,
			OffMaximum: p.OffMaximum,
		})
	}
	return
}

// ConvOffGradientRuleArrayPbToEntity []*messageGrpc.OffGradientRule => []ctype.OffGradientRule
func ConvOffGradientRuleArrayPbToEntity(params []*messageGrpc.OffGradientRule) (vals ctype.OffGradientRuleArray) {
	for _, p := range params {
		offMaximum := util.Int32IfThenElse(p.OffMaximum == 0, -1, p.OffMaximum)
		// offMaximum=-1代表无限制
		vals = append(vals, ctype.OffGradientRule{
			Price:      p.Price,
			Quota:      p.Quota,
			OffMaximum: offMaximum,
		})
	}
	return
}
